# the minimum version of CMake.
cmake_minimum_required (VERSION 3.4.1)
project (marsnapi)

set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE PATH "Installation directory" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(CMAKE_C_STANDARD 11)

message(STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}")

set(NAPI_MARS_PATH ${CMAKE_SOURCE_DIR}/mars)
set(SELF_LIB_NAME marsnapi)
set(APPLY_PATCH_RESULT 1) # 定义判断patch合入成功的标志

# Automatically apply patche under Linux
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
    message(STATUS "is linux os")
    set(MY_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
    get_filename_component(UP_ONE_DIR ${MY_CURRENT_SOURCE_DIR} DIRECTORY)
    get_filename_component(UP_TWO_DIR ${UP_ONE_DIR} DIRECTORY)
    get_filename_component(UP_THREE_DIR ${UP_TWO_DIR} DIRECTORY)
    get_filename_component(UP_FOUR_DIR ${UP_THREE_DIR} DIRECTORY)
    message(STATUS "UP_FOUR_DIR location is  ${UP_FOUR_DIR}")
    execute_process(
		 COMMAND bash automatically_apply_mars_patch_files.sh
         RESULT_VARIABLE APPLY_PATCH_RESULT
         WORKING_DIRECTORY ${UP_FOUR_DIR}
        )
endif() # CMAKE_HOST_SYSTEM_NAME MATCHES "Linux"

# 当patch合入成功，执行后面编译
# Windows、macOS请务必先手动合入patch，再执行编译，否则会报错
if(APPLY_PATCH_RESULT EQUAL 0 OR CMAKE_HOST_SYSTEM_NAME MATCHES "Windows" OR CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
    message(STATUS "The patch application was successful, and compilation is now starting")
    # Determine the type of system processor
    if(OHOS_ARCH MATCHES "armeabi-v7a")
        set(ANDROID_ABI "armeabi-v7a")
    elseif(OHOS_ARCH MATCHES "arm64-v8a")
        set(ANDROID_ABI "arm64-v8a")
    elseif(OHOS_ARCH MATCHES "x86_64")
        set(ANDROID_ABI "x86_64")
    endif() # OHOS_ARCH MATCHES "armeabi-v7a"

    add_definitions(-DOHOS_MARS_BUILD)
    add_definitions (-Wno-format-security)

    include_directories(${NAPI_MARS_PATH}/openssl/include)
	include_directories(${NAPI_MARS_PATH}/../thirdparty/openssl/${ANDROID_ABI}/include)
    add_subdirectory(${NAPI_MARS_PATH}/comm comm)
    add_subdirectory(${NAPI_MARS_PATH}/boost boost)
    add_subdirectory(${NAPI_MARS_PATH}/app app)
    add_subdirectory(${NAPI_MARS_PATH}/baseevent baseevent)
    add_subdirectory(${NAPI_MARS_PATH}/log xlog)
    add_subdirectory(${NAPI_MARS_PATH}/sdt sdt)
    add_subdirectory(${NAPI_MARS_PATH}/stn stn)

    # for zstd
    option(ZSTD_BUILD_STATIC "BUILD STATIC LIBRARIES" ON)
    option(ZSTD_BUILD_SHARED "BUILD SHARED LIBRARIES" OFF)
    set(ZSTD_SOURCE_DIR "${NAPI_MARS_PATH}/zstd")
    set(LIBRARY_DIR ${ZSTD_SOURCE_DIR}/lib)
    include(GNUInstallDirs)
    add_subdirectory(${NAPI_MARS_PATH}/zstd/build/cmake/lib zstd)

    include(${NAPI_MARS_PATH}/comm/CMakeUtils.txt)
    include_directories(.)
    include_directories(..)

    set(SELF_LIBS_OUT ${CMAKE_SYSTEM_NAME}.out)
    set(CMAKE_BUILD_WITH_INSTALL_RPATH true)

    if(OHOS)
        find_library(log-lib log)
        find_library(z-lib z)
        link_directories(
                        ${NAPI_MARS_PATH}/app
                        ${NAPI_MARS_PATH}/baseevent
                        ${NAPI_MARS_PATH}/sdt
                        ${NAPI_MARS_PATH}/stn
                        ${NAPI_MARS_PATH}/comm
                        ${NAPI_MARS_PATH}/boost
                        ${NAPI_MARS_PATH}/xlog
                        ${NAPI_MARS_PATH}/zstd)
        file(GLOB NAPI_INTERFACES napi_interfaces/*.cc)
        file(GLOB DEMO_NAPI_FILES demonapi/*.cpp)
        file(GLOB SELF_SRC_FILES ${NAPI_MARS_PATH}/libraries/mars_ohos_sdk/napi/*.cc)
        add_library(${SELF_LIB_NAME} SHARED
                    ${NAPIDEMO_SOURCE_FILES}
                    ${SELF_SRC_FILES}
                    ${DEMO_NAPI_FILES}
                    ${NAPI_INTERFACES})
        install(TARGETS ${SELF_LIB_NAME} LIBRARY DESTINATION ${SELF_LIBS_OUT} ARCHIVE DESTINATION ${SELF_LIBS_OUT})
        link_directories(${SELF_LIBS_OUT})
            if(NOT MSVC)
                set(CMAKE_FIND_LIBRARY_PREFIXES "lib")
                set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
            endif() # NOT MSVC
        find_library(CRYPT_LIB crypto PATHS ${NAPI_MARS_PATH}/openssl/openssl_lib_android/${ANDROID_ABI} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
        find_library(SSL_LIB ssl PATHS ${NAPI_MARS_PATH}/openssl/openssl_lib_android/${ANDROID_ABI} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)    
        target_link_libraries(${SELF_LIB_NAME} PUBLIC
                                    libace_napi.z.so
                                    libc++.a
                                    libcrypt.a
                                    libhilog_ndk.z.so
                                    stn
                                    sdt
                                    app
                                    baseevent
                                    comm
                                    mars-boost
                                    xlog
                                    libzstd_static
									${SSL_LIB}
                                    ${CRYPT_LIB}
                                    uv)
    endif() # OHOS
endif() # APPLY_PATCH_RESULT EQUAL 0 OR CMAKE_HOST_SYSTEM_NAME MATCHES "Windows" OR CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin"